﻿using System.ServiceModel;
using System.ServiceProcess;
using gixoom.Target.WcfWsHttp.Attributes;

namespace gixoom.Target.WcfWsHttp
{
    internal class gixoomWcfWsHttpTarget: ServiceBase
    {
        private ServiceHost serviceHost;
        private ITargetBlockStorage targetBlockStorage;

        /// <summary>
        /// Creates gixoomWcfWsHttpTarget object.
        /// </summary>
        /// <param name="name">the target name</param>
        /// <param name="targetBlockStorage">the target block storage</param>
        /// <exception cref="System.ArgumentNullException">One of the arguments is a null reference.</exception>
        /// <exception cref="System.ArgumentException"><i>name</i> is an empty string.</exception>
        public gixoomWcfWsHttpTarget([gixoomWcfWsHttpTargetName] string name,
            ITargetBlockStorage targetBlockStorage)
        {
            Ensure.ArgumentNotNull(name, "name");
            Ensure.StringArgumentNotEmpty(name, "name");
            Ensure.ArgumentNotNull(targetBlockStorage, "targetBlockStorage");

            ServiceName = name;
            this.targetBlockStorage = targetBlockStorage;
        }

        protected override void OnStart(string[] args)
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            serviceHost = new ServiceHost(targetBlockStorage);
            serviceHost.Open();
        }

        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }
}
